package cn.italkcloud.cph.schedule;

import cn.italkcloud.cph.constants.ReportTimeRangeEnum;
import cn.italkcloud.cph.service.VosService;
import cn.italkcloud.cph.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author dominate
 * @since 2024/7/9
 **/
@Slf4j
@Component
public class VosSyncSchedule {

    @Resource
    private VosService vosService;


    @Value("${schedule.cron.vos.enable}")
    private boolean enable;

    /**
     * 同步VOS数据
     */
    @Scheduled(cron = "${schedule.cron.vos.sync-fee}")
    @Async
    public void syncFee() {
        if (!enable) {
            return;
        }
        log.info("VosSyncSchedule sync");
        vosService.syncCustomerAndFee();
    }

    @Scheduled(cron = "${schedule.cron.vos.calculate-report}")
    @Async
    public void calculateTodayReport() {
        if (!enable) {
            return;
        }
        log.info("VosSyncSchedule Calculate Report");
        ReportTimeRangeEnum reportTimeRange = ReportTimeRangeEnum.MINUTE_5;
        Date startTime = DateUtil.getDayBeginOfDate();
        // 避免获取到不完整的时间段
        Date endTime = new Date(System.currentTimeMillis() - reportTimeRange.getSecond() * 1000);
        vosService.calculateReport(startTime, endTime, ReportTimeRangeEnum.MINUTE_5);
    }

    @Scheduled(cron = "${schedule.cron.vos.check-report}")
    @Async
    public void checkTodayReport() {
        if (!enable) {
            return;
        }
        Date today = new Date();
        log.info("VosSyncSchedule Check Today Report");
        vosService.mergeReport(DateUtil.getDateBegin(today), DateUtil.getDateEnd(today));
    }
}
